<?php

class TasktemplatesController extends AppController
{

    var $name = 'Tasktemplates';

    function index()
    {
        $this->Tasktemplate->recursive = 0;
        $this->set('tasktemplates', $this->paginate());
    }

    function view($id = null)
    {
        if( !$id )
        {
            $this->Session->setFlash(__('Invalid tasktemplate', true), 'error');
            $this->redirect(array('action' => 'index'));
        }
        $this->set('tasktemplate', $this->Tasktemplate->read(null, $id));
    }

    function add()
    {
        if( !empty($this->data) )
        {
            $this->Tasktemplate->create();
            if( $this->Tasktemplate->save($this->data) )
            {
                $this->Session->setFlash(__('The tasktemplate has been added successfully', true), 'success');
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The tasktemplate could not be added. Please, try again.', true), 'notice');
            }
        }
    }

    function edit($id = null)
    {
        if( !$id && empty($this->data) )
        {
            $this->Session->setFlash(__('Invalid tasktemplate', true), 'error');
            $this->redirect(array('action' => 'index'));
        }
        if( !empty($this->data) )
        {
            if( $this->Tasktemplate->save($this->data) )
            {
                $this->Session->setFlash(__('The tasktemplate has been edited successfully', true), 'success');
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The tasktemplate could not be edited. Please, try again.', true), 'notice');
            }
        }
        if( empty($this->data) )
        {
            $this->data = $this->Tasktemplate->read(null, $id);
        }
    }

    function delete($id = null)
    {
        if( !$id )
        {
            $this->Session->setFlash(__('Invalid tasktemplate', true), 'error');
            $this->redirect(array('action' => 'index'));
        }
        if( $this->Tasktemplate->delete($id) )
        {
            $this->Session->setFlash(__('Tasktemplate deleted', true), 'success');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Tasktemplate was not deleted', true), 'notice');
        $this->redirect(array('action' => 'index'));
    }

    function admin_index()
    {
        if( isset($this->params['named']['ajax']) && $this->params['named']['ajax'] == '1' )
        {
            //pr($this->params);
            $this->autoRender = false;

            // get how many rows we want to have into the grid - rowNum parameter in the grid
            $limit = $this->params['url']['rows'];

            // get index row - i.e. user click to sort. At first time sortname parameter -
            // after that the index from colModel
            $sidx = $this->params['url']['sidx'];

            // sorting order - at first time sortorder
            $sord = $this->params['url']['sord'];

            $page = $this->params['url']['page'];

            // if we not pass at first time index use the first column for the index or what you want
            if( !$sidx ) $sidx = 1;

            // calculate the number of rows for the query. We need this for paging the result
            $row = $this->Tasktemplate->find('count');
            $count = $row;

            // calculate the total pages for the query
            if( $count > 0 )
            {
                $total_pages = ceil($count / $limit);
            }
            else
            {
                $total_pages = 0;
            }

            // if for some reasons the requested page is greater than the total
            // set the requested page to total page
            if( $page > $total_pages ) $page = $total_pages;

            // calculate the starting position of the rows
            $start = $limit * $page - $limit;

            // if for some reasons start position is negative set it to 0
            // typical case is that the user type 0 for the requested page
            if( $start < 0 ) $start = 0;

            // the actual query for the grid data
            $limit_range = $start . "," . $limit;
            $sort_range = $sidx . " " . $sord;

            $this->Tasktemplate->recursive = -1;

            if( $this->params['url']['_search'] == 'true' )
            {
                $result = $this->Tasktemplate->find('all', array(
                    'fields' => array('id', 'name'),
                    'conditions' => array('Tasktemplate.name LIKE' => '%' . $this->params['url']['name'] . '%'),
                    'order' => $sort_range,
                    'limit' => $limit_range
                ));
            }
            else
            {
                $result = $this->Tasktemplate->find('all', array(
                    'fields' => array('id', 'name'),
                    'order' => $sort_range,
                    'limit' => $limit_range
                ));
            }

            $i = 0;
            $response->page = $page;
            $response->total = $total_pages;
            $response->records = $count;

            foreach($result as $result)
            {
                $response->rows[$i]['id'] = $result['Tasktemplate']['id'];

                $name = "<a href='" . APP_URL . "admin/tasktemplates/view/" . $result['Tasktemplate']['id'] . "'>" . $result['Tasktemplate']['name'] . "</a>";
                $view = "<a title='view' href='" . APP_URL . "admin/tasktemplates/view/" . $result['Tasktemplate']['id'] . "'>view</a>";
                $edit = "<a title='edit' href='" . APP_URL . "admin/tasktemplates/edit/" . $result['Tasktemplate']['id'] . "'>edit</a>";
                $delete = "<a title='delete' href=\"#\" onclick=\"if(confirm('Are you sure you want to delete this ?')){window.location.href= '" . APP_URL . "admin/tasktemplates/delete/" . $result['Tasktemplate']['id'] . "'}\">delete</a>";
                $actions = $view . "    " . $edit . "    " . $delete;

                $response->rows[$i]['cell'] = array($name, $actions);
                $i++;
            }

            echo json_encode($response);
            //exit;
        }
    }

    function admin_view($id = null)
    {
        if( !$id )
        {
            $this->Session->setFlash(__('Invalid tasktemplate', true), 'error');
            $this->redirect(array('action' => 'index'));
        }
        $this->set('tasktemplate', $this->Tasktemplate->read(null, $id));
    }

    function admin_add()
    {
        if( !empty($this->data) )
        {
            $this->Tasktemplate->create();
            if( $this->Tasktemplate->save($this->data) )
            {
                $this->Session->setFlash(__('The tasktemplate has been added successfully', true), 'success');
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The tasktemplate could not be added. Please, try again.', true), 'notice');
            }
        }
    }

    function admin_edit($id = null)
    {
        if( !$id && empty($this->data) )
        {
            $this->Session->setFlash(__('Invalid tasktemplate', true), 'error');
            $this->redirect(array('action' => 'index'));
        }
        if( !empty($this->data) )
        {
            if( $this->Tasktemplate->save($this->data) )
            {
                $this->Session->setFlash(__('The tasktemplate has been edited successfully', true), 'success');
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The tasktemplate could not be edited. Please, try again.', true), 'notice');
            }
        }
        if( empty($this->data) )
        {
            $this->data = $this->Tasktemplate->read(null, $id);
        }
    }

    function admin_delete($id = null)
    {
        if( !$id )
        {
            $this->Session->setFlash(__('Invalid tasktemplate', true), 'error');
            $this->redirect(array('action' => 'index'));
        }
        if( $this->Tasktemplate->delete($id) )
        {
            $this->Session->setFlash(__('Tasktemplate deleted', true), 'success');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Tasktemplate was not deleted', true), 'notice');
        $this->redirect(array('action' => 'index'));
    }

}
?>